<?php
/**
* 回调页面
* @author Jamers
* @since 2016.12.31
*/
include_once('libs/__INIT.php');

// 假设企业号在公众平台上设置的参数如下
$encodingAesKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
$token = "QDG6eK";
$corpId = "wx5823bf96d3bd56c7";
$corpSecret = "testtest";

$msg_signature = "";
$timestamp = 0;
$nonce = 0;
$echostr = "";
if (isset($_GET['msg_signature'])) $msg_signature = $_GET['msg_signature'];
if (isset($_GET['timestamp'])) $timestamp = intval($_GET['timestamp']);
if (isset($_GET['nonce'])) $nonce = intval($_GET['nonce']);
if (isset($_GET['echostr'])) $echostr = $_GET['echostr'];

//测试echostr验证
$msg_signature = "5c45ff5e21c57e6ad56bac8758b79b1d9ac89fd3";
$timestamp = "1409659589";
$nonce = "263014780";
$echostr = "P9nAzCzyDtyTWESHep1vC5X9xho/qYX3Zpb4yKa9SKld1DsH3Iyt3tP3zNdtp+4RPcs8TgAE7OaBO+FZXvnaqQ==";

if ($msg_signature && $timestamp && $nonce) {
    $qywx = new Qywx($corpId,$corpSecret,$token,$encodingAesKey);
    $qywx->LoadClass('ALL');
    var_dump($qywx);
    if ($echostr) {
        //回调验证
        exit($qywx->Common->echostrDecrypt($msg_signature,$timestamp,$nonce,$echostr));
    }else{
        //正常处理消息
        $rawdata = file_get_contents('php://input');
        $data = $qywx->Common->DecryptMsg($msg_signature,$timestamp,$nonce,$rawdata);
        $qywx->Common->SaveLogs($data);
        $msg = $qywx->Common->parsexml($data);
        //加载相应需要的库
        $qywx->LoadClass('Agent');
        $qywx->LoadClass(array('Agent','Ment'));
        //接收到相应消息，自行处理
        //...
    }
}else{
    echo 'Access Denyed!';
}
